/*
 * kylin-os-manager
 *
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "mainwindow.h"

#include <QDebug>
#include <QPluginLoader>
#include <QEventLoop>
#include <QGSettings>
#include "size_para.h"
#include "buried_point.hpp"
#include "toolutils.h"

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{

    mainIcon = new QPushButton(this);
    mainIcon->setFixedSize(64, 64);

    contentTitleLabel = new QLabel(this);
    contentTitleLabel->setFixedSize(420, 44);
    QFont font;
    font.setBold(true);
    font.setPixelSize(24);
    contentTitleLabel->setFont(font);
    contentTitleLabel->setText(tr("Detect Network Faults"));
    statusLabel = new CustomLabel(this);
    statusLabel->setFixedSize(420, 26);
    statusLabel->setText(tr("Detect and resolve Network Faults"));
    QVBoxLayout *mVLayoutLab = new QVBoxLayout;
    mVLayoutLab->setSpacing(0);
    mVLayoutLab->setContentsMargins(0, 0, 0, 5);
    mVLayoutLab->addStretch();
    mVLayoutLab->addWidget(contentTitleLabel);
    mVLayoutLab->addWidget(statusLabel);
    mVLayoutLab->addStretch();

    cancelBtn = new QPushButton(this);
    cancelBtn->setFixedHeight(36);
    cancelBtn->setMinimumWidth(96);
    cancelBtn->setText(tr("Cancel"));
    checkAgainBtn = new QPushButton(this);
    checkAgainBtn->setFixedHeight(36);
    checkAgainBtn->setMinimumWidth(96);
    //    checkAgainBtn->setFixedSize(110,36);
    checkAgainBtn->setText(tr("Restart"));
    //    startCheckBtn = new QPushButton(topWidget);
    //    startCheckBtn->setFixedSize(110,36);
    //    startCheckBtn->setText(tr("Start Check"));

    QVBoxLayout *mVLayoutBtn = new QVBoxLayout;
    mVLayoutBtn->setSpacing(0);
    mVLayoutBtn->setContentsMargins(0, 0, 0, 0);
    //    mVLayoutBtn->addWidget(startCheckBtn);
    mVLayoutBtn->addWidget(cancelBtn);
    mVLayoutBtn->addWidget(checkAgainBtn);

    returnBtn = new QPushButton(this);
    returnBtn->setFixedHeight(36);
    returnBtn->setMinimumWidth(96);
    //    returnBtn->setFixedSize(110,36);
    returnBtn->setText(tr("Return"));
    connect(returnBtn, &QPushButton::clicked, this, &MainWindow::slotReturnBtnClick);

    QHBoxLayout *mHLayoutTop = new QHBoxLayout;
    mHLayoutTop->setSpacing(0);
    mHLayoutTop->setContentsMargins(40, 24, 40, 0);
    mHLayoutTop->addWidget(mainIcon);
    mHLayoutTop->addSpacing(16);
    mHLayoutTop->addLayout(mVLayoutLab);
    mHLayoutTop->addStretch();
    mHLayoutTop->addWidget(returnBtn);
    mHLayoutTop->addSpacing(8);
    mHLayoutTop->addLayout(mVLayoutBtn);

    QWidget *midWidget = new QWidget(this);
    midWidget->setFixedHeight(20);
    line = new QFrame(midWidget);
    //    line->setFixedSize(720,2);
    line->setFixedHeight(2);
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    line->raise();
    progressBar = new KProgressBar(midWidget);
    progressBar->setFixedHeight(16);
    progressBar->setTextVisible(false);
    progressBar->setState(ProgressBarState::NormalProgress);
    progressBar->setMinimum(0);
    progressBar->setMaximum(120);
    progressBar->setOrientation(Qt::Horizontal);
    progressBar->setValue(0);
    //    progressBar->setStyleSheet("background-color: transparent;"
    //                               "QProgress::chunk{color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, "
    //                               "stop:0 rgba(62, 132, 255, 255), "
    //                               "stop:0.5 rgba(15, 167, 255, 255), "
    //                               "stop:1 rgba(30, 241, 251, 255));}");

    QVBoxLayout *mVLayoutMid = new QVBoxLayout;
    mVLayoutMid->setSpacing(0);
    mVLayoutMid->setContentsMargins(0, 0, 0, 0);
    mVLayoutMid->addStretch();
    mVLayoutMid->addWidget(progressBar);
    mVLayoutMid->addWidget(line);
    mVLayoutMid->addStretch();
    midWidget->setLayout(mVLayoutMid);

    QLabel *mIconLabel = new QLabel(this);
    mIconLabel->setFixedSize(16, 16);
    QIcon ico(QIcon::fromTheme("ukui-network-agent-symbolic"));
    QPixmap pix = ico.pixmap(ico.actualSize(QSize(16, 16)));
    mIconLabel->setPixmap(pix);
    mIconLabel->setProperty("useIconHighlightEffect", 0x2);

    mTitleLabel = new CustomLabel(this);
    mTitleLabel->setFixedSize(106, 26);
    font.setBold(true);
    font.setPixelSize(16);
    mTitleLabel->setFont(font);
    mTitleLabel->setText(tr("NetCheck"));
    QLabel *mItemSumLabel = new QLabel(this);
    mItemSumLabel->setFixedSize(186, 24);
    // QPalette pal;
    // pal.setColor(QPalette::Text, QColor("#262626"));
    // mItemSumLabel->setPalette(pal);
    // font.setBold(false);
    font.setPixelSize(12);
    mItemSumLabel->setText(tr("total 7 items"));
    QPushButton *expandBtn = new QPushButton(this);
    expandBtn->setFixedSize(16, 16);
    expandBtn->setIcon(QIcon::fromTheme("ukui-up-symbolic"));
    expandBtn->setVisible(false);
    QHBoxLayout *mTitleLayout = new QHBoxLayout;
    mTitleLayout->setSpacing(0);
    mTitleLayout->setContentsMargins(40, 0, 40, 7);
    mTitleLayout->addSpacing(0);
    mTitleLayout->addWidget(mIconLabel);
    mTitleLayout->addSpacing(10);
    mTitleLayout->addWidget(mTitleLabel);
    mTitleLayout->addSpacing(10);
    mTitleLayout->addWidget(mItemSumLabel);
    mTitleLayout->addStretch();
    mTitleLayout->addWidget(expandBtn);
    mTitleLayout->addSpacing(0);

    checkWidget = new QWidget();
    //    checkWidget->setFixedWidth(720);
    //    checkWidget->setMinimumHeight(252);
    //    checkWidget->setStyleSheet("background-color:green;");
    widgetView = new QVBoxLayout(checkWidget);
    widgetView->setSpacing(0);
    widgetView->setContentsMargins(0, 0, 0, 0);
    widgetView->setAlignment(Qt::AlignTop);
    //    checkWidget->setLayout(widgetView);

    scrollAreaWidgetContents = new QWidget(this);
    //    scrollAreaWidgetContents->setFixedWidth(720);
    //    scrollAreaWidgetContents->setMinimumHeight(256);
    //    scrollAreaWidgetContents->setStyleSheet("background-color:red;");

    allView = new QVBoxLayout(scrollAreaWidgetContents);
    allView->setSpacing(0);
    allView->setContentsMargins(0, 0, 0, 0);
    allView->setAlignment(Qt::AlignTop);
    allView->addWidget(checkWidget);
    //    scrollAreaWidgetContents->setLayout(allView);

    scrollArea = new QScrollArea(this);
    //    scrollArea->setSizeAdjustPolicy(QAbstractScrollArea::SizeAdjustPolicy::AdjustToContents);
    scrollArea->setFrameShape(QFrame::NoFrame);
    //    scrollArea->setFixedSize(720,400);
    scrollArea->setFixedHeight(400);
    scrollArea->setWidget(scrollAreaWidgetContents);
    scrollArea->setWidgetResizable(true);


    //    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    QHBoxLayout *mScroHLayout = new QHBoxLayout;
    mScroHLayout->setSpacing(0);
    mScroHLayout->setContentsMargins(40, 0, 40, 0);
    mScroHLayout->addSpacing(26);
    mScroHLayout->addWidget(scrollArea);


    QVBoxLayout *mAllLayout = new QVBoxLayout;
    mAllLayout->setSpacing(0);
    mAllLayout->setContentsMargins(0, 0, 0, 40);
    mAllLayout->addLayout(mHLayoutTop);
    mAllLayout->addSpacing(14);
    mAllLayout->addWidget(midWidget);
    mAllLayout->addSpacing(14);
    mAllLayout->addLayout(mTitleLayout);
    mAllLayout->addLayout(mScroHLayout);
    mAllLayout->addStretch();
    this->setLayout(mAllLayout);

    qRegisterMetaType<CHECKRESULT>("CHECKRESULT");
    qRegisterMetaType<statusStruct>("statusStruct");
    qRegisterMetaType<InnerNetCheck>("InnerNetCheck&");
    qRegisterMetaType<QMap<QString, bool>>("QMap<QString,bool>");
    qRegisterMetaType<QMap<QString, QMap<QString, bool>>>("QMap<QString,QMap<QString,bool>>");

    m_variableItems.clear();
    QRect availableGeometry = qApp->primaryScreen()->availableGeometry();
    this->move((availableGeometry.width() - this->width()) / 2, (availableGeometry.height() - this->height()) / 2);
    //    this->setFixedSize(824,600);

    loadingPlugins();
    initDBus();
    initSettings();
    initUI();

    //    connect(startCheckBtn, SIGNAL(clicked()), this, SLOT(startCheckProcess()));
    connect(cancelBtn, SIGNAL(clicked()), this, SLOT(manualStopCheckProcess()));
    connect(checkAgainBtn, SIGNAL(clicked()), this, SLOT(reCheckProcess()));

    //    this->show();
    //    UKUI_WIN_CONTROL_PROTOOL(this->winId());
}
MainWindow::~MainWindow()
{
    if (m_checkSettings) {
        delete m_checkSettings;
        m_checkSettings = nullptr;
    }
    if (m_themeSettings) {
        delete m_themeSettings;
        m_themeSettings = nullptr;
    }
    for (ItemWidget *it : m_variableItems) {
        if (it) {
            delete it;
            it = nullptr;
        }
    }
    m_variableItems.clear();
    if (m_ipTitle) {
        delete m_ipTitle;
        m_ipTitle = nullptr;
    }
    if (m_webTitle) {
        delete m_webTitle;
        m_webTitle = nullptr;
    }
    //这个补充可能是非必要的尝试
    //    for(PluginKits kit :m_pluginList){
    //        delete kit.m_pluginPtr;
    //    }
    //    this->close();
}
//读取插件
void MainWindow::loadingPlugins()
{
    for (int i = 0; i <= static_cast<int>(CheckType::NET_CHECK); ++i) {
        pluginKitsIncrease();
    }

    // TODO:要将动态库改为安装路径
    static bool installed = (QCoreApplication::applicationDirPath() == QDir(("/usr/bin")).canonicalPath());
    qDebug() << "MainWindow::loadingPlugins installed:" << installed;
    if (installed) {
        m_pluginsDir = QDir("/usr/lib/");
    } else {
        m_pluginsDir = QDir(qApp->applicationDirPath() + "/../../plugins/network-check/plugins");
        qDebug() << "application path:" << m_pluginsDir.absolutePath();
    }
    qDebug() << "MainWindow::loadingPlugins ________________:flag";
    foreach (QString fileName, m_pluginsDir.entryList(QDir::Files)) {
        qDebug() << "Scan Plugin: " << fileName;

        if (!fileName.endsWith(".so")) {
            continue;
        }
        // TODO:读取超时
        if (!loadingTimer) {
            loadingTimer = new QTimer(this);
        }
        loadingTimer->setInterval(1 * 1000);
        connect(loadingTimer, &QTimer::timeout, this, [=]() {
            qCritical() << "读取插件" << fileName << "超时，程序退出！";
        });
        loadingTimer->start();
        QPluginLoader loader(m_pluginsDir.absoluteFilePath(fileName));
        QObject *plugin = loader.instance();

        loadingTimer->stop();
        delete loadingTimer;
        loadingTimer = nullptr;
        if (plugin) {
            LibBase *pluginInstance = dynamic_cast<LibBase *>(plugin);
            if (pluginInstance->getKYdynamicLibKey() == "KYAppCheckKey") {
                int type = static_cast<int>(pluginInstance->getPluginType());
                qDebug() << "current plugin type is :" << type;

                switch (type) {
                case static_cast<int>(CheckType::NW_HW_DEVICE_CONF): {
                    HWCheck *instance = dynamic_cast<HWCheck *>(plugin);
                    if (type < m_pluginList.size()) {
                        m_pluginList[static_cast<int>(pluginInstance->getPluginType())].setInit(instance);
                        connect(m_pluginList[static_cast<int>(pluginInstance->getPluginType())].m_pluginObserver,
                                SIGNAL(updateMainWinUI(const statusStruct &)), this,
                                SLOT(updateMainWinUISlot(const statusStruct &)));
                    }
                    break;
                }
                case static_cast<int>(CheckType::NW_CON_CONF): {
                    IPCheck *instance = dynamic_cast<IPCheck *>(plugin);
                    if (type < m_pluginList.size()) {
                        m_pluginList[static_cast<int>(pluginInstance->getPluginType())].setInit(instance);
                        connect(m_pluginList[static_cast<int>(pluginInstance->getPluginType())].m_pluginObserver,
                                SIGNAL(updateMainWinUI(const statusStruct &)), this,
                                SLOT(updateMainWinUISlot(const statusStruct &)));
                    }
                    break;
                }
                case static_cast<int>(CheckType::DHCP_CONF): {
                    DHCPCheck *instance = dynamic_cast<DHCPCheck *>(plugin);
                    if (type < m_pluginList.size()) {
                        m_pluginList[static_cast<int>(pluginInstance->getPluginType())].setInit(instance);
                        connect(m_pluginList[static_cast<int>(pluginInstance->getPluginType())].m_pluginObserver,
                                SIGNAL(updateMainWinUI(const statusStruct &)), this,
                                SLOT(updateMainWinUISlot(const statusStruct &)));
                    }
                    break;
                }
                case static_cast<int>(CheckType::DNS_CONF): {
                    DNSCheck *instance = dynamic_cast<DNSCheck *>(plugin);
                    if (type < m_pluginList.size()) {
                        m_pluginList[static_cast<int>(pluginInstance->getPluginType())].setInit(instance);
                        connect(m_pluginList[static_cast<int>(pluginInstance->getPluginType())].m_pluginObserver,
                                SIGNAL(updateMainWinUI(const statusStruct &)), this,
                                SLOT(updateMainWinUISlot(const statusStruct &)));
                    }
                    break;
                }
                case static_cast<int>(CheckType::HOST_CONF): {
                    HostCheck *instance = dynamic_cast<HostCheck *>(plugin);
                    if (type < m_pluginList.size()) {
                        m_pluginList[static_cast<int>(pluginInstance->getPluginType())].setInit(instance);
                        connect(m_pluginList[static_cast<int>(pluginInstance->getPluginType())].m_pluginObserver,
                                SIGNAL(updateMainWinUI(const statusStruct &)), this,
                                SLOT(updateMainWinUISlot(const statusStruct &)));
                    }
                    break;
                }
                    //                case static_cast<int>(CheckType::PROXY_CONF): {
                    //                    ProxyCheck *instance = dynamic_cast<ProxyCheck *>(plugin);
                    //                    if (type < m_pluginList.size()) {
                    //                        m_pluginList[static_cast<int>(pluginInstance->getPluginType())].setInit(instance);
                    //                        connect(m_pluginList[static_cast<int>(pluginInstance->getPluginType())].m_pluginObserver,
                    //                                SIGNAL(updateMainWinUI(const statusStruct &)), this,
                    //                                SLOT(updateMainWinUISlot(const statusStruct &)));
                    //                    }
                    //                    break;
                    //                }
                case static_cast<int>(CheckType::NET_CHECK): {
                    NetCheck *instance = dynamic_cast<NetCheck *>(plugin);
                    qDebug() << "++++++++++MainWindow::loadingPlugins load CheckType::NET_CHECK++++++++++++";
                    instance->setInitialCheckType(m_innerCheckArgSettings);
                    if (type < m_pluginList.size()) {
                        m_pluginList[static_cast<int>(pluginInstance->getPluginType())].setInit(instance);
                        connect(m_pluginList[static_cast<int>(pluginInstance->getPluginType())].m_pluginObserver,
                                SIGNAL(updateMainWinUI(const statusStruct &)), this,
                                SLOT(updateMainWinUISlot(const statusStruct &)));
                        connect(instance, SIGNAL(detailCheckRes(QMap<QString, QMap<QString, bool>>)), this,
                                SLOT(refreshInnerCheckRes(QMap<QString, QMap<QString, bool>>)));
                        connect(m_pluginList[static_cast<int>(pluginInstance->getPluginType())].m_UIPtr,
                                SIGNAL(showContent()), this, SLOT(showContent()));
                    }
                    break;
                }
                default:
                    qCritical() << "【异常】打开了非检测工具插件文件！文件名：" << fileName;
                    break;
                }
            } else {
                continue;
            }

        } else {
            //如果加载错误且文件后缀为so，输出错误
            if (fileName.endsWith(".so"))
                qWarning() << fileName << "Load Failed: " << loader.errorString() << "\n";
        }
    }
}

void MainWindow::initUI()
{
    this->setAutoFillBackground(true);
    this->setBackgroundRole(QPalette::Base);

    mainIcon->setIcon(QIcon(":/data/normal.png"));
    mainIcon->setIconSize(TITLE_MAIN_SIZE);
    mainIcon->setFixedSize(TITLE_MAIN_SIZE);
    mainIcon->setStyleSheet(BTN_TO_LABEL_STYLE);

    //    ui->titleBar->setTitleName(tr("NetWork Check Tools"));

    //    setWindowTitle(tr("NetWork Check Tools"));

    setProgressBarVisible(false);
    //    m_quadsBtn = new QuadBtnsTitleBar(this);
    //    connect(m_quadsBtn,SIGNAL(showConfigureWin()),this,SLOT(showConfigureWin()));
    //    m_quadsBtn->setShowBtnsMode(QuadBtnsShowMode::NO_MAX_BTN,this);
    //    ui->quadBtnsTitleBarLayout->addWidget(m_quadsBtn);
    //    ui->quadBtnsTitleBarLayout->addStretch();
    int points = m_pluginList.size();
    qDebug() << "共有 " << points << " 检查项。";
    cancelBtn->hide();
    checkAgainBtn->hide();
    checkAgainBtn->setProperty("isImportant", true);
    returnBtn->hide();
    //    startCheckBtn->show();
    //    startCheckBtn->setProperty("isImportan",true);
    widgetView->addSpacing(0);
    for (int i = 0; i < points; ++i) {
        if (m_pluginList[i].m_UIPtr) {
            widgetView->addWidget(m_pluginList[i].m_UIPtr);
            m_pluginList[i].m_pluginPtr->setInit();
            //            widgetView->addSpacing(4);
        } else {
            qCritical() << "加载了非正确的动态库！";
            continue;
        }
    }
    connect(this, SIGNAL(startCheckIndex(int)), this, SLOT(pluginStartCheck(int)), Qt::QueuedConnection);
    widgetView->addSpacing(10);
    //    widgetView->addStretch();
    //    allView->addStretch();
}

void MainWindow::initDBus()
{
    // TODO:最小化后拉起界面
    //    m_NWMDBus = new NmDbusAdaptor();
}

void MainWindow::initSettings()
{
    const QByteArray ukuiStyle(UKUI_STYLE);
    if (QGSettings::isSchemaInstalled(ukuiStyle)) {
        m_themeSettings = new QGSettings(ukuiStyle);
        connect(m_themeSettings, &QGSettings::changed, this, [=](const QString &key) {
            if (key == "styleName") {
                m_curStyle = m_themeSettings->get("styleName").toString();
                themeUIRefresh();
            }
        });
        m_curStyle = m_themeSettings->get("styleName").toString();
        themeUIRefresh();
    }

    ToolUtils toolUtils;
    m_innerCheckArgSettings.isInnerCheck = toolUtils.getInnerCheckSetting();
    QMap<QString, QVector<QString>> settingsMap = toolUtils.getConfigFile();
    QList<QString> ipStrList;
    QList<QString> webStrList;
    if (settingsMap.contains("config-ip")) {
        QVector<QString> ipVector = settingsMap.value("config-ip");
        ipStrList.clear();
        ipStrList = ipVector.toList();
    }
    if (settingsMap.contains("config-web")) {
        QVector<QString> webVector = settingsMap.value("config-web");
        webStrList.clear();
        webStrList = webVector.toList();
    }
    m_innerCheckArgSettings.ipClear();
    for (int i = 0; i < ipStrList.size(); ++i) {
        if (!(ipStrList.at(i)).isEmpty()) {
            m_innerCheckArgSettings.ip[i] = ipStrList.at(i);
            ++m_innerCheckArgSettings.ipNum;
        }
    }
    m_innerCheckArgSettings.webClear();
    for (int i = 0; i < webStrList.size(); ++i) {
        if (!(webStrList.at(i)).isEmpty()) {
            m_innerCheckArgSettings.web[i] = webStrList.at(i);
            ++m_innerCheckArgSettings.webNum;
        }
    }
}
//开始检查函数
void MainWindow::startCheckProcess()
{
    refreshUI();
    emit startCheckIndex(0); //从第一项开始检查
    m_correctItem = 0;
    m_warningItem = 0;
    m_errorItem = 0;
    setProgressBarVisible(true);
    progressBar->setValue(0);
    contentTitleLabel->setText(tr("Checking..."));
    statusLabel->setText(tr("Start"));

    cancelBtn->show();
    checkAgainBtn->hide();
    returnBtn->hide();
    //    startCheckBtn->hide();

    m_goOnCheck = true;

    kdk::kabase::BuriedPoint buriedPoint;
    if (buriedPoint.functionBuriedPoint(kdk::kabase::AppName::KylinNetworkCheck,
                                        kdk::kabase::BuriedPoint::PT::KylinNetworkCheckStartCheck)) {
        qCritical() << "buried point fail!pt:BaseInfo";
    }
}

void MainWindow::errStopCheckProcess()
{
    m_goOnCheck = false;
    cancelBtn->hide();
    returnBtn->show();
    checkAgainBtn->show();
    //    startCheckBtn->hide();
    contentTitleLabel->setText(tr("Check Result"));

    statusLabel->setText(tr("Find %1 issues,%2 is error,repair and start").arg(m_errorItem).arg(m_warningItem));

    mainIcon->setIcon(QIcon(":/data/errFinished.png"));
    setProgressBarVisible(false);
    progressBar->setValue(0);
    //    m_isNetAllRight = true;
    m_correctItem = 0;
    m_warningItem = 0;
    m_errorItem = 0;
}

void MainWindow::manualStopCheckProcess()
{
    statusLabel->setText(tr("Stopping, please wait!"));
    m_goOnCheck = false;
    cancelBtn->setEnabled(false);
    //停止时无法直接中断，需当前检测项结束后才能中断，故阻塞，直到手动停止信号发送。
    connect(this, SIGNAL(manualStoped()), this, SLOT(slotManualStopCheck()), Qt::UniqueConnection);
}
void MainWindow::slotManualStopCheck()
{
    cancelBtn->hide();
    checkAgainBtn->show();
    returnBtn->show();
    //    startCheckBtn->hide();
    checkAgainBtn->setEnabled(true);
    //    startCheckBtn->setEnabled(false);
    m_goOnCheck = false;
    contentTitleLabel->setText(tr("Check Result"));
    if (m_warningItem == 0 && m_errorItem == 0) {
        statusLabel->setText(tr("Pause，checked %1 item，no issue").arg(m_correctItem));
        mainIcon->setIcon(QIcon(":/data/allOKFinished.png"));
    } else {
        statusLabel->setText(tr("Pause，find %1 issues，repair and start").arg(m_warningItem + m_errorItem));
        mainIcon->setIcon(QIcon(":/data/errFinished.png"));
    }

    setProgressBarVisible(false);
    progressBar->setValue(0);
    m_correctItem = 0;
    m_warningItem = 0;
    m_errorItem = 0;
}
void MainWindow::reCheckProcess()
{
    //    refreshUI();
    startCheckProcess();
}

void MainWindow::showConfigureWin()
{
    qDebug() << "MainWindow::showConfigureWin m_goOnCheck:" << m_goOnCheck;
    //如果正在检测，则点击设置无效
    if (m_goOnCheck) {
        qDebug() << "检测过程中，禁止设置！";
        return;
    }
    //新建设置界面并移动到窗体中央
    ConfigWin configureWin;
    connect(&configureWin, SIGNAL(sigUpdateConfigFile()), this, SLOT(slotUpdateConfigFile()));
    QPoint windowCenter = QPoint(this->mapToGlobal(QPoint(0, 0)).x() + this->width() / 2,
                                 this->mapToGlobal(QPoint(0, 0)).y() + this->height() / 2);
    configureWin.move(windowCenter - configureWin.rect().center());
    configureWin.activateWindow();
    configureWin.showWin();
    //界面刷新靠gsettings变化，无需再处理
}
void MainWindow::slotUpdateConfigFile()
{
    qDebug() << "MainWindow::slotUpdateConfigFile";
    ToolUtils toolUtils;
    m_innerCheckArgSettings.isInnerCheck = toolUtils.getInnerCheckSetting();
    QMap<QString, QVector<QString>> settingsMap = toolUtils.getConfigFile();
    QList<QString> ipStrList;
    QList<QString> webStrList;
    if (settingsMap.contains("config-ip")) {
        QVector<QString> ipVector = settingsMap.value("config-ip");
        ipStrList.clear();
        ipStrList = ipVector.toList();
        qDebug() << "MainWindow::slotUpdateConfigFile config-ip:" << ipStrList;
    }
    if (settingsMap.contains("config-web")) {
        QVector<QString> webVector = settingsMap.value("config-web");
        webStrList.clear();
        webStrList = webVector.toList();
        qDebug() << "MainWindow::slotUpdateConfigFile config-web:" << webStrList;
    }
    m_innerCheckArgSettings.ipClear();
    for (int i = 0; i < ipStrList.size(); ++i) {
        if (!(ipStrList.at(i)).isEmpty()) {
            m_innerCheckArgSettings.ip[i] = ipStrList.at(i);
            ++m_innerCheckArgSettings.ipNum;
        }
    }
    m_innerCheckArgSettings.webClear();
    for (int i = 0; i < webStrList.size(); ++i) {
        if (!(webStrList.at(i)).isEmpty()) {
            m_innerCheckArgSettings.web[i] = webStrList.at(i);
            ++m_innerCheckArgSettings.webNum;
        }
    }
    refreshUI();
}
void MainWindow::allFinished()
{
    m_goOnCheck = false;
    cancelBtn->hide();
    checkAgainBtn->show();
    returnBtn->show();
    //    startCheckBtn->hide();
    contentTitleLabel->setText(tr("Check Result"));
    setProgressBarVisible(false);
    progressBar->setValue(0);
    if (m_errorItem == 0) {
        mainIcon->setIcon(QIcon(":/data/allOKFinished.png"));
    } else {
        mainIcon->setIcon(QIcon(":/data/errFinished.png"));
    }
    //这一部分处理放到UI刷新的回调里了，在那里完成了statuslabel的显示修改
    if (m_warningItem == 0 && m_errorItem == 0) {
        statusLabel->setText(tr("checked %1 items，no issue").arg(m_correctItem));
    } else {
        statusLabel->setText(tr("find %1 errs,%2 issues,repair and start").arg(m_errorItem).arg(m_warningItem));
    }
    //    m_isNetAllRight = true;
}

void MainWindow::themeUIRefresh()
{
    if ("ukui-dark" == m_curStyle || "ukui-black" == m_curStyle) {
        QPalette linePalette = line->palette();
        QColor lineColor = QColor(255, 255, 255);
        lineColor.setAlphaF(0.08);
        linePalette.setColor(QPalette::Light, lineColor);
        line->setPalette(linePalette);

        QPalette statusLabelPalette = line->palette();
        statusLabelPalette.setColor(QPalette::Text, QColor(89, 89, 89, 255));
        statusLabel->setPalette(statusLabelPalette);
    } else {
        QPalette linePalette = line->palette();
        QColor lineColor = QColor(0, 0, 0);
        lineColor.setAlphaF(0.08);
        linePalette.setColor(QPalette::Dark, lineColor);
        line->setPalette(linePalette);
        QPalette statusLabelPalette = line->palette();
        statusLabelPalette.setColor(QPalette::Text, QColor(89, 89, 89, 255));
        statusLabel->setPalette(statusLabelPalette);
    }
}
//开启内网检测，检测完毕后，点显示详情后显示的区域
void MainWindow::showContent()
{
    //根据区域的显示状态，决定显示还是隐藏
    if (m_ipTitle) {
        if (m_ipTitle->isHidden()) {
            m_ipTitle->show();
        } else {
            m_ipTitle->hide();
        }
    }
    if (m_webTitle) {
        if (m_webTitle->isHidden()) {
            m_webTitle->show();
        } else {
            m_webTitle->hide();
        }
    }
    if (isShow) {
        for (auto &item : m_variableItems) {
            item->hide();
        }
    } else {
        for (auto &item : m_variableItems) {
            item->show();
        }
    }
    isShow = !isShow;
}
//根据检测结果，刷新详情区域内容
void MainWindow::refreshInnerCheckRes(QMap<QString, QMap<QString, bool>> resMap)
{

    //清空原区域
    for (auto &item : m_variableItems) {
        delete item;
    }
    m_variableItems.clear();
    if (m_ipTitle) {
        delete m_ipTitle;
        m_ipTitle = nullptr;
    }
    if (m_webTitle) {
        delete m_webTitle;
        m_webTitle = nullptr;
    }

    //    QMap<QString,bool> baiduRes;
    QMap<QString, bool> ipRes;
    QMap<QString, bool> webRes;
    if (resMap.isEmpty()) {
        qWarning() << "MainWindow::refreshInnerCheckRes resMap.isEmpty!";
    } else {
        //        baiduRes.clear();
        ipRes.clear();
        webRes.clear();
        //        baiduRes = resMap.value("baidu",baiduRes);
        ipRes = resMap.value("ip", ipRes);
        webRes = resMap.value("web", webRes);
    }
    //添加IP部分的窗口
    if (!ipRes.isEmpty()) {
        m_ipTitle = new ItemWidget(scrollAreaWidgetContents);
        m_ipTitle->setStatusCheck(CheckStatus::EVERTHING_IS_OK);
        m_ipTitle->setCheckRes(tr("Intranet IP"), tr(""));
        allView->addWidget(m_ipTitle);
        m_ipTitle->hide();
    }
    QMap<QString, bool>::iterator ipIter = ipRes.begin();
    while (ipIter != ipRes.end()) {
        ItemWidget *temp = new ItemWidget(scrollAreaWidgetContents);
        if (ipIter.value()) {
            temp->setCheckRes(ipIter.key(), "OK");
            temp->setStatusCheck(CheckStatus::EVERTHING_IS_OK);
        } else {
            temp->setCheckRes(ipIter.key(), "ERR");
            temp->setStatusCheck(CheckStatus::ERROR);
        }
        allView->addWidget(temp);
        m_variableItems.append(temp);
        temp->hide();
        ++ipIter;
    }
    //添加web部分的窗口
    if (!webRes.isEmpty()) {
        m_webTitle = new ItemWidget(scrollAreaWidgetContents);
        m_webTitle->setStatusCheck(CheckStatus::EVERTHING_IS_OK);
        m_webTitle->setCheckRes(tr("Intranet Web"), tr(""));
        allView->addWidget(m_webTitle);
        m_webTitle->hide();
    }
    QMap<QString, bool>::iterator webIter = webRes.begin();
    while (webIter != webRes.end()) {
        ItemWidget *temp = new ItemWidget(scrollAreaWidgetContents);
        if (webIter.value()) {
            temp->setCheckRes(webIter.key(), "OK");
            temp->setStatusCheck(CheckStatus::EVERTHING_IS_OK);
        } else {
            temp->setCheckRes(webIter.key(), "ERR");
            temp->setStatusCheck(CheckStatus::ERROR);
        }
        allView->addWidget(temp);
        m_variableItems.append(temp);
        temp->hide();
        ++webIter;
    }
}
//刷新主界面插件部分UI
//入参为各插件检测结果
void MainWindow::updateMainWinUISlot(const statusStruct &curStatus)
{
    qDebug() << "更新项为:" << curStatus.m_index;
    qDebug() << "更新项名称为:" << curStatus.m_projectName;
    qDebug() << "项描述信息为:" << curStatus.m_projectDetail;
    qDebug() << "项检测状态为:" << curStatus.m_projectRes;
    qDebug() << "项检测结果为:" << (int)curStatus.m_curStutus;

    m_pluginList[curStatus.m_index].m_UIPtr->statusChanged(curStatus);

    //必须有下面这行，否则界面不会刷新！！！
    m_pluginList[curStatus.m_index].m_UIPtr->repaint();
    //必须有上面这行，否则界面不会刷新！！！

    // netcheck为最后一项，需要特殊处理
    if (curStatus.m_index == static_cast<int>(CheckType::NET_CHECK)) {
        qDebug() << "MainWindow::updateMainWinUISlot : CheckType::NET_CHECK";
        if (curStatus.m_curStutus == CheckStatus::INIT) {
            statusStruct temp = curStatus;
            if (m_innerCheckArgSettings.isInnerCheck) {
                qDebug() << "通过设置切换为内网检测！";
                temp.m_projectName = QString(tr("InnerNet Check"));
                temp.m_projectDetail = QString(tr("Check whether the intranet is smooth"));
            } else {
                qDebug() << "通过设置切换为外网检测！";
                temp.m_projectName = QString(tr("Internet access"));
                temp.m_projectDetail = QString(tr("Can user browse out net?"));
            }
            m_pluginList[curStatus.m_index].m_UIPtr->statusChanged(temp);
        }
    }
    //自动检测下一项
    if ((curStatus.m_curStutus == CheckStatus::EVERTHING_IS_OK) || (curStatus.m_curStutus == CheckStatus::WARNING)
        || (curStatus.m_curStutus == CheckStatus::ERROR)) {

        if (curStatus.m_curStutus == CheckStatus::WARNING) {
            ++m_warningItem;
            qDebug() << "MainWindow::updateMainWinUISlot CheckStatus::WARNING：" << m_warningItem;

        } else if (curStatus.m_curStutus == CheckStatus::ERROR) {
            ++m_errorItem;
            qDebug() << "MainWindow::updateMainWinUISlot CheckStatus::ERROR:" << m_errorItem;

        } else {
            ++m_correctItem;
        }
        progressBar->setValue(20 + 20 * curStatus.m_index);
        //============如果要错误停止，应该在这个地方停止==================
        if (curStatus.m_index == 0 && m_errorItem != 0) {
            errStopCheckProcess();
            return;
        }


        if (m_warningItem == 0 && m_errorItem == 0) {
            statusLabel->setText(tr("checked %1 items,no issue").arg(curStatus.m_index + 1));
        } else {
            statusLabel->setText(tr("checked %1 items,find %2 errs,%3 issues")
                                     .arg(curStatus.m_index + 1)
                                     .arg(m_errorItem)
                                     .arg(m_warningItem));
        }

        //通过m_goOnCheck控制是否继续检测（即点击取消的flag）
        if (m_goOnCheck) {
            if (curStatus.m_index < m_pluginList.size() - 1) {
                qDebug() << "MainWindow::updateMainWinUISlot next check start :" << curStatus.m_index + 1;
                m_pluginList[curStatus.m_index + 1].m_pluginPtr->startChecking(m_innerCheckArgSettings);
            }
            if (curStatus.m_index == m_pluginList.size() - 1) {
                //设计要求进度条满了之后要停留1s，否则看不出来进度100%，之后立即变0%
                QEventLoop loop;
                QTimer::singleShot(1000, &loop, SLOT(quit()));
                loop.exec();
                allFinished();
            }
        } else {
            qDebug() << "MainWindow::updateMainWinUISlot m_goOnCheck:false";
            emit manualStoped();
        }


    } else {
        qDebug() << "第" << curStatus.m_index << "项检测中";
    }
}
//从第几项开始检测（为后续暂停检测，又继续检测的接口）
void MainWindow::pluginStartCheck(int index)
{
    m_pluginList[index].m_pluginPtr->startChecking(m_innerCheckArgSettings);
}
//新建结构体塞入list
void MainWindow::pluginKitsIncrease()
{
    PluginKits temp;
    temp.m_pluginObserver = nullptr;
    temp.m_pluginPtr = nullptr;
    temp.m_UIPtr = nullptr;
    m_pluginList.append(temp);
    qDebug() << "m_pluginList 大小为：" << m_pluginList.size();
}
//重置UI
void MainWindow::refreshUI()
{
    //标题部分UI重置
    //    ui->titleBar->setTitleName(tr("NetWork Check Tools"));
    isShow = false;
    mainIcon->setIcon(QIcon(":/data/normal.png"));
    statusLabel->setText(tr("Detect and resolve Network Faults"));
    contentTitleLabel->setText(tr("Detect Network Faults"));
    cancelBtn->hide();
    returnBtn->hide();
    cancelBtn->setEnabled(true);
    checkAgainBtn->hide();
    //    startCheckBtn->show();
    //    startCheckBtn->setEnabled(true);
    progressBar->setValue(0);
    progressBar->setState(ProgressBarState::NormalProgress);
    m_correctItem = 0;
    m_warningItem = 0;
    m_errorItem = 0;
    //插件部分UI重置
    for (auto &it : m_pluginList) {
        it.m_pluginPtr->setInit();
    }
    //详情部分UI重置
    for (auto &item : m_variableItems) {
        delete item;
    }
    m_variableItems.clear();
    //    if(m_baiduTitle){
    //        delete m_baiduTitle;
    //        m_baiduTitle = nullptr;
    //    }
    if (m_ipTitle) {
        delete m_ipTitle;
        m_ipTitle = nullptr;
    }
    if (m_webTitle) {
        delete m_webTitle;
        m_webTitle = nullptr;
    }
}
void MainWindow::setProgressBarVisible(bool isBarShow)
{
    if (isBarShow) {
        progressBar->setVisible(true);
        line->setVisible(false);
    } else {
        progressBar->setVisible(false);
        line->setVisible(true);
    }
}
void MainWindow::slotReturnBtnClick()
{
    emit sigChangeStackWidgetMain(0);
}
void MainWindow::changeThemeColor(int color)
{
    if (color) { //深色

    } else {
    }
}
void MainWindow::changeSystemSize(int size)
{
    mFontSize = size;
    QFont font;

    int labelSize = 18 * mFontSize / 11;
    font.setPixelSize(labelSize);
    mTitleLabel->setFont(font);

    for (auto &it : m_pluginList) {
        it.m_UIPtr->setSystemFontSizeChange(mFontSize);
    }
}
